const koa = require('koa');
const app  = new koa();
const mongoose = require('mongoose');
const jwt = require('jsonwebtoken');
const jwtKoa = require('koa-jwt');
const util = require('util');
const verify = util.promisify(jwt.verify) // 解密
const secret = 'jwt demo';

const bodyParser = require('koa-bodyparser')
app.use(bodyParser());

const cors = require('koa2-cors')
app.use(cors())

const Router = require('koa-router');
let router = new Router();
let user = require('./appApi/User.js');
let content = require('./appApi/Content.js');

//页面初始化不需要验证token
app.use(jwtKoa({secret}).unless({
        path: [/^\/user\/login/,/^\/content\/allItem/,/^\/user\/allUsers/] //数组中的路径不需要通过jwt验证
    }))


router.use('/user',user.routes());
router.use('/content',content.routes());

app.use(router.routes());
app.use(router.allowedMethods());




//启动服务时候，链接数据库
const { connect,initSchemas }  = require('./dataBase/init.js');
;(async ()=>{
    await connect();
    initSchemas();

})()


app.listen(3000,()=>{
    console.log('statring at port 3000');
})